package com.example.laixunlong.tools;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpGetPost {
    public static String data="";//文本类型
    public static String URL="";//请求路径
    public static InputStream input=null;

    /*网络请求文本文件*/
    public static String GetMapping(String path)
    {
        URL=path;
        new Thread(){
            @Override
            public void run(){
                try {
                    URL url=new URL(URL);/*"https://laixunlong.github.io/key.json"*/
                    URLConnection urlConnection= url.openConnection();
                    urlConnection.connect();
                    InputStream is=urlConnection.getInputStream();
                    InputStreamReader br=new InputStreamReader(is);
                    char[] chars=new char[1024];
                    int length=-1;
                    StringBuffer str=new StringBuffer();
                    while ( (length =br.read(chars)) != -1 )
                    {
                        str.append(chars,0,length);
                    }
                    Log.e("接收内容",str.toString());
                    data=str.toString();
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        }.start();
        return data;
    }
    /*图片文件*/
    public static InputStream PostMapping(String path)
    {
        URL=path;
        new Thread(){
            @Override
            public void run(){
                FileOutputStream f=null;
                try {
                    URL url=new URL(URL);//取地址
                    URLConnection connection=url.openConnection();//发请求
                    input=connection.getInputStream();//创建文件流

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
        return input;
    }
    /*视频文件*/
}
